package com.kindergarten.service.impl;

import com.kindergarten.mapper.RoleMapper;
import com.kindergarten.mapper.UserMapper;
import com.kindergarten.pojo.User;
import com.kindergarten.pojo.UserRole;
import com.kindergarten.mapper.UserRoleMapper;
import com.kindergarten.service.IUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bruce
 * @since 2022-11-02
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    @Resource
    RoleMapper roleMapper;

    @Resource
    UserMapper userMapper;

    @Resource
    UserRoleMapper userRoleMapper;

    @Override
    @Transactional
    public void addUserRole(Integer roleId, String[] userIds) {
        if(userIds.length != 0){
            for (String userId : userIds) {
                UserRole userRole = new UserRole();
                userRole.setRoleId(roleId);
                userRole.setUserId(userId);
                userRoleMapper.insert(userRole);

                User user=new User();
                user.setUserId(userId);
                user.setUserState(roleId);
                userMapper.updateById(user);
            }
        }
    }
}
